package com.wrench.elevator.tida.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wrench.elevator.tida.util.ExcelUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.wrench.elevator.tida.constant.Result;
import com.wrench.elevator.tida.domain.Page;
import com.wrench.elevator.tida.exception.WrenchException;
import com.wrench.elevator.tida.service.DTGLService;
import com.wrench.elevator.tida.util.WrenchUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/elevator_manage")
public class ElevatorManageController {
	@Resource
	private DTGLService dTGLService;

	private static final Logger logger = LoggerFactory
			.getLogger(ElevatorManageController.class);

	@RequestMapping(value = "/batchAddZhuangDanyuan", method = RequestMethod.POST)
	public void batchAddZhuangDanyuan(HttpServletRequest request,
									  HttpServletResponse response, ModelMap map){
		logger.debug("==>**********enter in batchInputUser(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String building_id = WrenchUtil.getPara(request, "building_id");
		MultipartFile file = WrenchUtil.getFile(request, "file");
		String[] keys = { "zhuang_name", "danyuan_name" };
		logger.debug("==> params : building_id = {} , file = {} ", building_id,
				file);
		WrenchException error=null;
		Map<String, String>[] value = null;
		try {
			value = ExcelUtil.analyseExcel(file.getInputStream(), keys);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		t:
		for (Map<String, String> m : value) {
			try {
				dTGLService.addZhuangDanyuan(building_id, m.get("zhuang_name"),
						m.get("danyuan_name"), map);
			} catch (WrenchException e) {
				// TODO Auto-generated catch block
				error = e;
				break t;
			}
			if(!map.get("flag").equals("SUCCESS")){
				error=new WrenchException(Result.OTHER_ERROR_CODE,Result.OTHER_ERROR_MESSAGE);
				break t;
			}
		}
		if (WrenchUtil.hasNull(error)) {

			map.clear();
			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("message", "inputUser success");
		} else {
			map.clear();
			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}

		logger.debug("==> response = {}", JSON.toJSON(map));
		WrenchUtil.responseMap(response, map);
		map = null;
		logger.debug("==>**********exit out batchInputUser(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
	}


	@RequestMapping(value = "/addZhuangDanyuan", method = RequestMethod.POST)
	public void addZhuangDanyuan(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in addZhuangDanyuan(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String building_id = WrenchUtil.getPara(request, "building_id");
		String zhuang_name = WrenchUtil.getPara(request, "zhuang_name");
		String danyuan_name = WrenchUtil.getPara(request, "danyuan_name");

		WrenchException error = null;
		try {
			dTGLService.addZhuangDanyuan(building_id, zhuang_name,
					danyuan_name, map);
		} catch (WrenchException e) {
			error = e;
		}
		if (WrenchUtil.hasNull(error)) {
			if (map.get("flag").equals("SUCCESS")) {
				map.clear();
				map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
				map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
				map.put("message", "addZhuangDanyuan success");
			} else {
				map.clear();
				map.put(Result.RESULT_CODE, Result.OTHER_ERROR_CODE);
				map.put(Result.RESULT_MESSAGE, Result.OTHER_ERROR_MESSAGE);
			}
		} else {
			map.clear();
			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		logger.debug("==> response = {}", JSON.toJSON(map));
		WrenchUtil.responseMap(response, map);
		map = null;
		logger.debug("==>**********exit out addZhuangDanyuan(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

	}

	@RequestMapping(value = "/queryDT2", method = RequestMethod.GET)
	public void queryDT2(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in queryDT2(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String building_name = WrenchUtil.getPara(request, "building_name");
		String zhuang_danyuan = WrenchUtil.getPara(request, "zhuang_danyuan");
		String current_page = WrenchUtil.getPara(request, "current_page");
		String limit = WrenchUtil.getPara(request, "limit");

		WrenchException error = null;
		Page page = null;
		try {
			page = dTGLService.queryDT2(building_name, zhuang_danyuan,
					current_page, limit);
		} catch (WrenchException e) {
			error = e;
		}

		if (WrenchUtil.hasNull(error)) {
			map.clear();
			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("message", "queryDT2 success");
			map.put("page", page);

		} else {
			map.clear();
			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		logger.debug("==> response = {}", JSON.toJSON(map));
		WrenchUtil.responseMap(response, map);
		map = null;

		logger.debug("==>**********exit out queryDT2(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
	}

	@RequestMapping(value = "/queryDT", method = RequestMethod.GET)
	public void queryDT(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in queryDT(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String name = WrenchUtil.getPara(request, "name");
		String current_page = WrenchUtil.getPara(request, "current_page");
		String limit = WrenchUtil.getPara(request, "limit");

		WrenchException error = null;
		Page page = null;
		try {
			page = dTGLService.queryDT(name, current_page, limit);
		} catch (WrenchException e) {
			error = e;
		}

		if (WrenchUtil.hasNull(error)) {
			map.clear();
			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("message", "queryDT success");
			map.put("page", page);

		} else {
			map.clear();
			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		logger.debug("==> response = {}", JSON.toJSON(map));
		WrenchUtil.responseMap(response, map);
		map = null;

		logger.debug("==>**********exit out queryDT(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
	}

	@RequestMapping(value = "/delDT", method = RequestMethod.POST)
	public void delDT(HttpServletRequest request, HttpServletResponse response,
			ModelMap map) {
		logger.debug("==>**********enter in delDT(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String elevator_id = WrenchUtil.getPara(request, "elevator_id");
		WrenchException error = null;
		try {
			dTGLService.delDT(elevator_id);
		} catch (WrenchException e) {
			error = e;
		}

		if (WrenchUtil.hasNull(error)) {

			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("message", "delRole success");

		} else {
			map.clear();
			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		logger.debug("==> response = {}", JSON.toJSON(map));
		WrenchUtil.responseMap(response, map);
		map = null;
		logger.debug("==>**********enter in delDT(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
	}

	@RequestMapping(value = "/addElevatorAndCamera", method = RequestMethod.POST)
	public void addElevatorAndCamera(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in addElevator(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String building_name = WrenchUtil.getPara(request, "building_name");
		String zhuang_danyuan = WrenchUtil.getPara(request, "zhuang_danyuan");
		String elevator_num = WrenchUtil.getPara(request, "elevator_num");
		String camera_num = WrenchUtil.getPara(request, "camera_num");
		logger.debug(
				"==> params :building_name = {} , zhaung_danyuan = {} , elevator_num = {} , camera_num = {}",
				building_name, zhuang_danyuan, elevator_num, camera_num);
		WrenchException error = null;
		try {
			dTGLService.addElevatorAndCamera(building_name, zhuang_danyuan,
					elevator_num, camera_num);
		} catch (WrenchException e) {
			error = e;
		}

		if (WrenchUtil.hasNull(error)) {

			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("message", "addElevatorAndCamera success");

		} else {
			map.clear();
			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		logger.debug("==> response = {}", JSON.toJSON(map));
		WrenchUtil.responseMap(response, map);
		map = null;

	}

	@RequestMapping(value = "/addElevator", method = RequestMethod.POST)
	public void addElevator(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in addElevator(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String building_name = WrenchUtil.getPara(request, "building_name");
		String zhuang_danyuan = WrenchUtil.getPara(request, "zhuang_danyuan");
		String elevator_num = WrenchUtil.getPara(request, "elevator_num");
		logger.debug(
				"==> params :building_name = {} , zhaung_danyuan = {} , elevator_num = {} ",
				building_name, zhuang_danyuan, elevator_num);

		WrenchException error = null;
		try {
			dTGLService.addElevator(building_name, zhuang_danyuan,
					elevator_num, map);
		} catch (WrenchException e) {
			error = e;
		}

		if (WrenchUtil.hasNull(error)) {
			Object flag = map.get("flag");
			map.clear();
			if ("SUCCESS".equals(flag)) {
				map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
				map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
				map.put("message", "addElevator success");
			} else {
				map.put(Result.RESULT_CODE, Result.OTHER_ERROR_CODE);
				map.put(Result.RESULT_MESSAGE, Result.OTHER_ERROR_MESSAGE);
			}

		} else {
			map.clear();
			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		logger.debug("==> response = {}", JSON.toJSON(map));
		WrenchUtil.responseMap(response, map);
		map = null;

		logger.debug("==>**********exit out addElevator(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
	}

	@RequestMapping(value = "/addCamera", method = RequestMethod.POST)
	public void addCamera(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in addCamera(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String building_name = WrenchUtil.getPara(request, "building_name");
		String zhuang_danyuan = WrenchUtil.getPara(request, "zhuang_danyuan");
		String camera_num = WrenchUtil.getPara(request, "camera_num");
		logger.debug(
				"==> params :building_name = {} , zhaung_danyuan = {} , camera_num = {} ",
				building_name, zhuang_danyuan, camera_num);

		WrenchException error = null;
		try {
			dTGLService.addCamera(building_name, zhuang_danyuan, camera_num,
					map);
		} catch (WrenchException e) {
			error = e;
		}

		if (WrenchUtil.hasNull(error)) {
			Object flag = map.get("flag");
			map.clear();
			if ("SUCCESS".equals(flag)) {
				map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
				map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
				map.put("message", "addCamera success");
			} else {
				map.put(Result.RESULT_CODE, Result.OTHER_ERROR_CODE);
				map.put(Result.RESULT_MESSAGE, Result.OTHER_ERROR_MESSAGE);
			}

		} else {
			map.clear();
			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		logger.debug("==> response = {}", JSON.toJSON(map));
		WrenchUtil.responseMap(response, map);
		map = null;

		logger.debug("==>**********exit out addCamera(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
	}
}
